package org.spamforces.web.page;

import com.codeforces.commons.text.Patterns;
import com.google.inject.Inject;
import org.spamforces.dao.*;
import org.spamforces.model.Broadcast;
import org.spamforces.model.BroadcastStatus;
import org.spamforces.model.RecipientProvider;
import org.spamforces.model.User;

import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author MikeMirzayanov (mirzayanovmr@gmail.com)
 */
@SuppressWarnings("ProtectedField")
public abstract class BroadcastPage extends WebPage {
    @Inject
    protected BroadcastDao broadcastDao;

    @Inject
    protected UserDao userDao;

    @Inject
    protected RecipientProviderDao recipientProviderDao;

    @Inject
    protected EmailDao emailDao;

    @Inject
    protected SmsDao smsDao;

    public static class BroadcastForView {
        private Broadcast fields;
        private User user;
        private List<RecipientProvider> recipientProviders;
        private String emailProvidersList;
        private boolean canBeDeleted;
        private String statusWithTime;
        private String scheduledTime;
        private int doneEmailCount;
        private int queueEmailCount;
        private int failedEmailCount;
        private int doneSmsCount;
        private int queueSmsCount;
        private int failedSmsCount;

        public void setFields(Broadcast fields) {
            this.fields = fields;
        }

        public Broadcast getFields() {
            return fields;
        }

        public void setUser(User user) {
            this.user = user;
        }

        public User getUser() {
            return user;
        }

        public List<RecipientProvider> getRecipientProviders() {
            return Collections.unmodifiableList(recipientProviders);
        }

        public void setEmailProvidersList(String emailProvidersList) {
            this.emailProvidersList = emailProvidersList;
        }

        public String getEmailProvidersList() {
            return emailProvidersList;
        }

        public void setCanBeDeleted(boolean canBeDeleted) {
            this.canBeDeleted = canBeDeleted;
        }

        public boolean isCanBeDeleted() {
            return canBeDeleted;
        }

        public void setStatusWithTime(String statusWithTime) {
            this.statusWithTime = statusWithTime;
        }

        public String getStatusWithTime() {
            return statusWithTime;
        }

        public void setScheduledTime(String scheduledTime) {
            this.scheduledTime = scheduledTime;
        }

        public String getScheduledTime() {
            return scheduledTime;
        }

        public int getDoneEmailCount() {
            return doneEmailCount;
        }

        public int getQueueEmailCount() {
            return queueEmailCount;
        }

        public int getFailedEmailCount() {
            return failedEmailCount;
        }

        public int getDoneSmsCount() {
            return doneSmsCount;
        }

        public int getQueueSmsCount() {
            return queueSmsCount;
        }

        public int getFailedSmsCount() {
            return failedSmsCount;
        }
    }

    @SuppressWarnings("AccessingNonPublicFieldOfAnotherObject")
    protected BroadcastForView toFullView(Broadcast broadcast) {
        BroadcastForView broadcastForView = new BroadcastForView();
        broadcastForView.setFields(broadcast);
        broadcastForView.setUser(userDao.find(broadcast.getUserId()));
        broadcastForView.recipientProviders = new ArrayList<>();
        broadcastForView.setCanBeDeleted(broadcast.getUserId() == getUser().getId());
        String[] s = Patterns.COMMA_PATTERN.split(broadcast.getEmailProvidersId());
        String t = "";
        for (String x : s) {
            String y = x.trim();
            try {
                long id = Integer.parseInt(y);
                RecipientProvider recipientProvider = recipientProviderDao.find(id);
                broadcastForView.recipientProviders.add(recipientProvider);
                if (!t.isEmpty()) {
                    t += ", \n";
                }
                t += recipientProvider.getTitle();
            } catch (Exception ignored) {
                // No operations.
            }
        }

        broadcastForView.emailProvidersList = t;
        broadcastForView.statusWithTime = broadcast.getStatus().toString();
        broadcastForView.scheduledTime = "";

        if (broadcast.getStatus() != BroadcastStatus.SCHEDULING) {
            broadcastForView.scheduledTime = DateFormat.getInstance().format(broadcast.getScheduledTime());

            EmailDao.Statistics emailStatistics = emailDao.findStatisticsByBroadcastId(broadcast.getId());
            broadcastForView.doneEmailCount = emailStatistics.getDoneCount();
            broadcastForView.queueEmailCount = emailStatistics.getQueueCount();
            broadcastForView.failedEmailCount = emailStatistics.getFailedCount();

            SmsDao.Statistics smsStatistics = smsDao.findStatisticsByBroadcastId(broadcast.getId());
            broadcastForView.doneSmsCount = smsStatistics.getDoneCount();
            broadcastForView.queueSmsCount = smsStatistics.getQueueCount();
            broadcastForView.failedSmsCount = smsStatistics.getFailedCount();

            broadcastForView.statusWithTime += ", " + broadcastForView.scheduledTime + '\n';
        }

        return broadcastForView;
    }

    protected List<BroadcastForView> toFullView(List<Broadcast> broadcasts) {
        List<BroadcastForView> broadcastsForView = new ArrayList<>();
        for (Broadcast broadcast : broadcasts) {
            broadcastsForView.add(toFullView(broadcast));
        }
        return broadcastsForView;
    }
}
